
## Appendix Replication code  ###########################################################################################################


#Alternative measure for foreign sponsorship Casey(2021)
modf2= glm(cus_fail~ allminority+ log_e_gdppc+ gdp_growth+ log_e_pop+ log_oil_income_pc+ partB+ mon+ persB+mil+
             any_spons+ factor(Country)+ factor(Year), data=minority_TSC, family="binomial")

modf4= glm(cus_fail~ minority+ log_e_gdppc+ gdp_growth+ log_e_pop+ log_oil_income_pc+ partB+ mon+ persB+mil+
             any_spons+ factor(Country)+ factor(Year), data=minority_TSC, family="binomial")

modf6= glm(cus_fail~ frac_minority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             any_spons+ factor(Country)+factor(Year),
           data=minority_TSC, family="binomial")

##Cluster Standard Errors by country 
modf2=coeftest(modf2, vcovCL, cluster= minority_TSC$Country)
modf4=coeftest(modf4, vcovCL, cluster= minority_TSC$Country)
modf6=coeftest(modf6, vcovCL, cluster= minority_TSC$Country)

stargazer( modf2,modf4,modf6, omit = c("Country","Year"), type = "text",
           title = "Table 2 with alternative measure for foreign sponsorship from Casey(2021)-1945-2010",
           covariate.labels = c("All minority regime", "Minority excluding majority","Minority excluding minorities", "Log GDP","GDP growth", "Log population", 
                                "Log oil wealth",
                                "Party", "Monarchy","Personalist","Military", "Foreign sponsorship Casey(2021)"),
           dep.var.caption = "Dependent Variable: Regime Breakdown",
           no.space = TRUE,  # Remove space between columns
           notes = c("Standard errors clustered by country"),
           model.names = FALSE,
           dep.var.labels.include = FALSE,
           order = c("allminority", "minority", "frac_minority", "log_e_gdppc", "gdp_growth", 
                     "log_oil_income_pc", "log_e_pop", "partB", "mon", "persB","mil",
                     "foreignsupport" ))




##############################
#  GWF (1946-2010) 

minority_TSC1946=minority_TSC %>% 
  filter(Year>1945 & Year<2011)

modg1= glm(cus_fail~ allminority, data=minority_TSC1946, family="binomial")
modg2= glm(cus_fail~ allminority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             foreignsupport+ factor(Country)+factor(Year), data=minority_TSC1946, family="binomial")

modg3= glm(cus_fail~ minority, data=minority_TSC1946, family="binomial")
modg4= glm(cus_fail~ minority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             foreignsupport+ factor(Country)+factor(Year), data=minority_TSC1946, family="binomial")

modg5= glm(cus_fail~ frac_minority, data=minority_TSC1946, family="binomial")
modg6= glm(cus_fail~ frac_minority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             foreignsupport+ factor(Country)+factor(Year), data=minority_TSC1946, family="binomial")

##Cluster Standard Errors by country 
modg1=coeftest(modg1, vcovCL, cluster= minority_TSC1946$Country)
modg2=coeftest(modg2, vcovCL, cluster= minority_TSC1946$Country)
modg3=coeftest(modg3, vcovCL, cluster= minority_TSC1946$Country)
modg4=coeftest(modg4, vcovCL, cluster= minority_TSC1946$Country)
modg5=coeftest(modg5, vcovCL, cluster= minority_TSC1946$Country)
modg6=coeftest(modg6, vcovCL, cluster= minority_TSC1946$Country)

stargazer( modg1,modg2, modg3,modg4, modg5,modg6, omit = c("Country","Year"),type = "text", 
           title = "Regime Breakdown using the original GWF dataset (1946-2010)",
           covariate.labels = c("All Minority Regime", "Minority excluding majority","Minority excluding Minorities", "Log GDP","GDP Growth", "Log Oil Wealth", "Log Population", 
                                "Party", "Monarchy","Personalist","Military","Foreign Support"),
           dep.var.caption = "Dependent Variable: Regime Breakdown",
           no.space = TRUE,  # Remove space between columns
           notes = c("Standard errors clustered by country"),
           add.lines=list(c("Country and Year FE", "No", "Yes", "No", "Yes","No","Yes")),
           model.names = FALSE,
           dep.var.labels.include = FALSE,
           order = c("allminority", "minority", "frac_minority", "log_e_gdppc", "gdp_growth", 
                     "e_total_oil_income_pc", "log_e_pop", "partB", "mon", "persB",
                     "Military", "foreignsupport" ))




########################################################################
##### USE Autocracies of the World data ####
#The "Autocracies of the World" dataset by B. Magaloni, J. Chu, and E. Min. Autocracies of the World. Dataset. 2013. http://cddrl.fsi.stanford.edu/research/autocracies_of_the_world_dataset.

#load (Data_set.xls)
mcm= Data_Set %>% filter(demo_r==0)

#create minority regimes variable
mcm <- mcm %>% 
  mutate(minority = ifelse(reg_id %in% c("43601", "43602", "45001", 
                                         "46102", "46103", "51602", "51604",
                                         "56001", "64501", "64502","64504", "64505", 
                                         "65203", "66301", "67801", "69201", "71301", "71302") |
                             (reg_id == "51703" & year > 1993 & year < 2013), 
                           yes = 1, 
                           no = 0))
mcm$minority=as.integer(mcm$minority)


#Adding variables 
vdem2= vdem %>% dplyr::select( COWcode,year,e_gdppc, e_pop, e_total_oil_income_pc, v2regimpgroup)
mcm= mcm %>% left_join(vdem2, by= c("ccode"= "COWcode", "year"= "year" ))
mcm=mcm %>% mutate(foreignsupport= ifelse(v2regimpgroup==13, 1, 0))
cem$log_oil_income_pc <- log(cem$e_total_oil_income_pc+0.001)


#Models
mc1= glm(change~ minority
         , data=cem, family="binomial")
mc2= glm(change~ minority+log_e_gdppc+ gdp_growth+ log_oil_income_pc+log_e_pop+ party+ mon+ pers+ foreignsupport
         + factor(country)+ factor(year)
         , data=cem, family="binomial")


mc1=coeftest(mc1, vcov=vcovCL, cluster= cem$country)
mc2=coeftest(mc2, vcov=vcovCL, cluster= cem$country)

stargazer(mc1, mc2, omit = c("country","year"),type = "text",
          title = "Table 2 using Autocracies of the World Dataset (Magaloni, Chu, Min; 2013)",
          covariate.labels = c("Minority excluding Majority", "Log GDP","GDP Growth", "Log Oil Wealth", "Log Population", 
                               "Party", "Monarchy","Personalist","Foreign support"),
          dep.var.caption = "Dependent Variable: Regime Breakdown",
          no.space = TRUE,  # Remove space between columns
          notes = c("Standard errors clustered by country"),
          add.lines=list(c("Observations", "4,490", "4,490"),
                         c("Country and Year FE", "NO", "Yes")),
          model.names = FALSE,
          dep.var.labels.include = FALSE)



########################################################################
#Alternate specification of Table 2:Remove Bahrain 
minority_TSCnoBAhrain <- minority_TSC %>% filter(Country != "Bahrain")

modb1= glm(cus_fail~ allminority, data=minority_TSC, family="binomial")
modb2= glm(cus_fail~ allminority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             factor(Country)+ factor(Year)
           , data=minority_TSCnoBAhrain, family="binomial")

modb3= glm(cus_fail~ minority, data=minority_TSC, family="binomial")
modb4= glm(cus_fail~ minority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             factor(Country)+ factor(Year)
           , data=minority_TSCnoBAhrain, family="binomial")

modb5= glm(cus_fail~ frac_minority, data=minority_TSC, family="binomial")
modb6= glm(cus_fail~ frac_minority+ log_e_gdppc+ gdp_growth+ log_oil_income_pc+ log_e_pop+ partB+ mon+ persB+mil+
             factor(Country)+ factor(Year)
           , data=minority_TSCnoBAhrain, family="binomial")

##Cluster Standard Errors by country 
modb1=coeftest(modb1, vcovHC, cluster= minority_TSCnoBAhrain$Country)
modb2=coeftest(modb2, vcovHC, cluster= minority_TSCnoBAhrain$Country)
modb3=coeftest(modb3, vcovHC, cluster= minority_TSCnoBAhrain$Country)
modb4=coeftest(modb4, vcovHC, cluster= minority_TSCnoBAhrain$Country)
modb5=coeftest(modb5, vcovHC, cluster= minority_TSCnoBAhrain$Country)
modb6=coeftest(modb6, vcovHC, cluster= minority_TSCnoBAhrain$Country)


stargazer( modb1,modb2, modb3,modb4, modb5,modb6,omit = c("Country", "Year"),type = "text",
           title = "Regime Breakdown, Bahrain Removed",
           covariate.labels = c("All Minority Regime", "Minority excluding majority","Minority excluding Minorities", "Log GDP","GDP Growth", "Log Oil Wealth", "Log Population", 
                                "Party", "Monarchy","Personalist","Military","Foreign Support"),
           dep.var.caption = "Dependent Variable: Regime Breakdown",
           no.space = TRUE,  # Remove space between columns
           notes = c("Standard errors clustered by country"),
           model.names = FALSE,
           add.lines=list(c("Country and Year FE", "No", "Yes", "No", "Yes","No","Yes")),
           dep.var.labels.include = FALSE,
           order = c("allminority", "minority", "frac_minority", "log_e_gdppc", "gdp_growth", 
                     "e_total_oil_income_pc", "log_e_pop", "partB", "mon", "persB",
                     "Military", "foreignsupport" ))




##########
#Figure A.1: Covariate Balance
newNames <- c(
  'log_e_gdppc' = 'GDP per capita (log)',
  'log_e_pop' = 'Population (log)',
  'foreignsupport' = 'foreign/colonial support',
  'e_civil_war'='Civil War',
  'decolonize' = 'Decolonization',
  'prev_partB' = 'Previous party',
  'prev_mon' = ' Previous Monarchy',
  'prev_persB' = ' Previous Personalist'
)

love.plot(m.out, thresholds = c(m = 0.1),
          threshold = 0.1,
          stat = 'mean.diffs',
          limits = c(-1, 1),
          var.names = newNames,
          position = "bottom",
)


###########
## Using Beissinger's Revolutionary Episodes Dataset

#Load Beissinger data

data_autoc= all_data %>% 
  filter(incumbgovdem==0)

## Create post-cold war variable 
data_autoc=data_autoc %>% 
  mutate(post_coldwar = ifelse(startyear>1992, yes = "1",
                               no = 0))

# merge variables from V Dem dataset

controlvdem=`V-Dem-CY-Full+Others-v12`%>% 
  select(country_name, COWcode, year, e_gdppc, e_pop)
controlvdem <- controlvdem %>%
  mutate(COWcode = if_else(country_name == "Yemen" ,679, COWcode ))
controlvdem$codyear= paste(controlvdem$COWcode, controlvdem$year, sep = "")
controlvdem=controlvdem %>% 
  select(-country_name, -COWcode,-year )
data_autoc= data_autoc %>% left_join(controlvdem, by= "codyear")

#models 
mod1= glm(successful~ minority,
          data=data_autoc, family="binomial")

mod2= glm(successful~ minority+ + log(e_gdppc)+ log(e_pop),
          data=data_autoc, family="binomial")

mod3= glm(successful~ minority+ + log(e_gdppc)+ log(e_pop)+ log(particnum)+ coalitionleadership+ armed+
            log(daysduration)+ post_coldwar,
          data=data_autoc, family="binomial")


stargazer(mod1, mod2,mod3,
          title = " Table 3 Using Beissinger's Revolutionary Episodes Dataset ",
          covariate.labels = c("Minority over Majority", "Log GDP", "Log population",
                               "Number of participants (log)",
                               "Coalitional Leadership",
                               "Armed campaign", "Campaign duration (log)",
                               "Post-cold war"),
          dep.var.caption = "Dependent Variable: Revolution Success",
          no.space = TRUE,  # Remove space between columns
          model.names = FALSE,
          dep.var.labels.include = FALSE)



###########
#Design Analysis
library(retrodesign)

mod1= lm(cus_fail~minority, data=minority_TSC)
summary(mod1)
D <- -0.03       
s <- 0.009729 
alpha <- 0.05   
df <- 5765
retrodesign(D , s, alpha, df )








